#!/bin/bash
#pMusic
#Copyright 2008,2009,2010,2011,2012,2013,2014
#Sigmund Berglund - Val�moen, Norway.

export TEXTDOMAIN=pmusic
MYPID=$$
. $CONFIG #in case user has changed settings in preferences

case "$1" in
-play_cd)
	$APPDIR/func_kill
	$APPDIR/func_store_playlist
	$APPDIR/func -new
	$APPDIR/func_cd -read_cd
	cp -f $WORKDIR/playlist_read_cd $WORKDIR/playlist
	if [ -s $WORKDIR/playlist ]; then
		head -n 1 $WORKDIR/playlist | cut -d'|' -f5 > $WORKDIR/PLAYLIST
		$APPDIR/func_player -play -autoplay
	fi
	echo true > $WORKDIR/UPDATE_GUI #update playlist when probing with the -c switch
	$APPDIR/func -statusbar_playlist & #update statusbar
	;;
-read_cd)
	#libcddb is much faster than using cdda2wav, so if available we use this
	if type cddb_query > /dev/null 2>&1 && [ $CDDB = true ]; then
		$APPDIR/func_cd -read_cd_libcddb
	else
		$APPDIR/func_cd -read_cd_cdrtools
	fi
	;;
-read_cd_libcddb)
	echo "$(gettext 'Probing disc')" > $WORKDIR/splashtext
	$APPDIR/box_splash &
	cddb_query -i $CD_DEVICE read > $WORKDIR/tmp_cd 2>&1
	if [ "`LANG=C grep -F 'error:' $WORKDIR/tmp_cd`" ]; then
		echo 100 > $WORKDIR/splash
		$APPDIR/func_cd -read_cd_cdrtools
	else
		ARTIST="`LANG=C grep -F 'Artist:' $WORKDIR/tmp_cd | cut -d: -f2 | cut -c 4-`"
		ALBUM="`LANG=C grep -F 'Title:' $WORKDIR/tmp_cd | cut -d: -f2 | cut -c 5-`"
		YEAR="`LANG=C grep -F 'Year:' $WORKDIR/tmp_cd | cut -d: -f2 | cut -c 6-`"
		NR=1
		GENRE="`LANG=C grep -F 'Genre:' $WORKDIR/tmp_cd | cut -d: -f2 | cut -c 5-`"
		echo -n > $WORKDIR/sourcelist
		echo -n > $WORKDIR/playlist_read_cd
		while read I; do
			[ ! "`grep -F '[' <<< "$I"`" ] && continue #not track information
			TRACK="`LANG=C cut -d"'" -f2- <<< "$I" | awk -F "' by " '{print $1}'`"
			MIN="$(tail -c7 <<< "$I" | cut -d')' -f1 | cut -d'(' -f2 | cut -d: -f1)"
			SEC="$(tail -c7 <<< "$I" | cut -d')' -f1 | cut -d'(' -f2 | cut -d: -f2)"
			LENGTH="`echo $(printf "%02d" $MIN):$(printf "%02d" $SEC)`"
			echo "cdda:${NR}: ${LENGTH}:${ARTIST} - ${TRACK}|${ARTIST}|${TRACK}|${ALBUM}|${NR}|${YEAR}|${GENRE}|||wav|1411kb|${LENGTH}|||gtk-audio_cd" >> $WORKDIR/sourcelist
			echo "|${LENGTH}|${ARTIST} - ${TRACK}|cdda:${NR}:|00000000000000000$(printf "%02d" ${NR}) cdda:${NR}: ${LENGTH}:${ARTIST} - ${TRACK}" >> $WORKDIR/playlist_read_cd
			NR=$(($NR+1))
		done < $WORKDIR/tmp_cd
		cp -f $WORKDIR/sourcelist $WORKDIR/cddb
		$APPDIR/func -add_to_history "pMusic: $(gettext 'CD audio')" & #update history
		echo true > $WORKDIR/UPDATE_SOURCELIST #update source-list
		echo 100 > $WORKDIR/splash
	fi
	;;
-read_cd_cdrtools)
	killall cdda2wav 2> $STDERR #in case playing from CD
	echo "$(gettext 'Probing disc')" > $WORKDIR/splashtext
	echo "kill -9 $MYPID" > $WORKDIR/splash_cancelcode
	$APPDIR/box_splash &
	echo -n > $WORKDIR/playlist_read_cd #-play_cd can skip -add_all
	#read internal cd-text info
	LANG=C cdda2wav dev=$CD_DEVICE -info-only -no-infofile > $WORKDIR/cd-cddb 2>&1 &
	while [ "$BREAK" != "true" ]; do
		sleep 1 #wait for disc to spin
		#is disc an audio-CD
		if [ "`grep -E "no audio|Cannot stat device|Read TOC size failed|load cdrom" $WORKDIR/cd-cddb`" ]; then
			ERROR="$(gettext 'No valid audio-CD inserted')"
			echo -n > $WORKDIR/cd-cddb
			BREAK=true
		fi
		#don't run complete cdda2wav check. kill it when we got enough
		if [ "`grep -F 'ISRC' $WORKDIR/cd-cddb`" ] || [ "`grep 'load cdrom' $WORKDIR/cd-cddb`" ] ; then
			BREAK=true
			for I in `ps -eo pid,command | grep -F cdda2wav | awk '{print $1}'`; do kill -9 $I 2> $STDERR; done
		fi
	done
	[ "`grep 'load cdrom' $WORKDIR/cd-cddb`" ] && ERROR="$(gettext 'No disc inserted')"
	#if no internal cd-text, try www
	if [ $CDDB = true ]; then
		LANG=C wget --spider --tries 1 -T 1 $PING_SERVER 2> $WORKDIR/cd-tmp #check internet connection
		if [ "`grep 'CD-Text: not detected' $WORKDIR/cd-cddb`" ] && [ "`grep 'connected' $WORKDIR/cd-tmp`" ]; then


#			DISC_ID="`grep -F 'CDINDEX discid:' $WORKDIR/cd-cddb | cut -d: -f2 | tr -d ' '`"
#			wget -q "http://musicbrainz.org/cdtoc/$DISC_ID" -O- > $WORKDIR/cd-musicbrainz_discid
#			RELEASE_URL="$(grep -m1 -F 'musicbrainz.org/release' $WORKDIR/cd-musicbrainz_discid | cut -d'"' -f2 | cut -d'"' -f1)"
#			wget -q "$RELEASE_URL" -O- > $WORKDIR/cd-musicbrainz_release


			cdda2wav dev=$CD_DEVICE -cddb=1 -info-only -no-infofile > $WORKDIR/cd-cddb 2>&1 &
			while [ ! "$BREAK2" ]; do
				#don't run complete cdda2wav check. kill it when we got enough
				if [ "`grep ISRC $WORKDIR/cd-cddb`" ] || [ "`grep 'load cdrom' $WORKDIR/cd-cddb`" ]; then
					BREAK2=true
				fi
			done
			for I in `ps -eo pid,command | grep cdda2wav | awk '{print $1}'`; do kill -9 $I 2> $STDERR; done
		fi
	fi
	
	#build cdda-list
	grep 'Track ' $WORKDIR/cd-cddb | grep \' | cut -d ':' -f 2- | cut -d '[' -f 1 | cut -c 3- | sed -e "s/.$//" > $WORKDIR/tmp_cd_tracks
	grep -F '.(' $WORKDIR/cd-cddb | grep -F ':' | tr '.' '\n' | tr ' ' '\n' | grep -F ':' > $WORKDIR/tmp_cd_length
	paste -d'|' $WORKDIR/tmp_cd_tracks $WORKDIR/tmp_cd_length > $WORKDIR/tmp_cd
	ALBUM=`grep 'Album title' $WORKDIR/cd-cddb | cut -d "'" -f 2`
	ARTIST=`grep 'Album title' $WORKDIR/cd-cddb | cut -d '[' -f 2 | cut -d ']' -f 1 | cut -d ' ' -f 2-`
	#add length
	echo -n > $WORKDIR/sourcelist
	NR=1
	while read I; do
		TRACK="$(cut -d'|' -f1 <<< "$I")"
		LENGTH="$(cut -d'|' -f2 <<< "$I")"
		[ "$LENGTH" = "0:00" ] && continue
		[ "`echo "$LENGTH" | wc -c`" = "5" ] && LENGTH=0$LENGTH #ensure 5digits
		if [ "$TRACK" ]; then
			echo "cdda:$NR: $LENGTH:$ARTIST - $TRACK|$ARTIST|$TRACK|$ALBUM|$NR|||||wav|1411kb|$LENGTH|||gtk-audio_cd" >> $WORKDIR/sourcelist
			echo "|$LENGTH|$ARTIST - $TRACK|cdda:$NR:|$(printf "%05d" $NR) cdda:$NR: $LENGTH:$ARTIST - $TRACK" >> $WORKDIR/playlist_read_cd #-play_cd can skip -add_all
		else
			#no cd-text/web-info available
			echo "cdda:$NR: $LENGTH:Track ${NR}|Track ${NR}||||||||wav|1411kb|$LENGTH|||gtk-audio_cd" >> $WORKDIR/sourcelist
			echo "|$LENGTH|Track ${NR} | CD audio|$(printf "%05d" $NR) cdda:$NR: $LENGTH:Track $NR" >> $WORKDIR/playlist_read_cd
		fi
		NR=$(($NR+1))
	done < $WORKDIR/tmp_cd
	[ ! -s $WORKDIR/sourcelist ] && echo "|$(gettext 'No tracks detected')" > $WORKDIR/sourcelist #no tracks detected
	cp -f $WORKDIR/sourcelist $WORKDIR/cddb
	$APPDIR/func -add_to_history "pMusic: $(gettext 'CD audio')" & #update history
	echo true > $WORKDIR/UPDATE_SOURCELIST #update source-list
	echo 100 > $WORKDIR/splash
	;;
esac
	